package vtc;

import java.util.Date;
import org.jfree.data.time.*;

public class DrawUnit extends OutputUnit
{
	private double[] events_;
	private Date[] timeTags_;
	private int position_; 
	private int bufferSize_;
	private String chartName_;
	private String xlabel_;
	private String ylabel_;
	private TimeSeriesDemo picture_;
	
	DrawUnit(String id, VtcArrayBlockingQueue outQueues[], int bufferSize, String chartName, String xlabel, String ylabel)
	{
		super(id, outQueues);
		events_ = new double[bufferSize];
		timeTags_ = new Date[bufferSize];
		position_ = 0;
		bufferSize_ = bufferSize;
		chartName_ = chartName;
		xlabel_ = xlabel;
		ylabel_ = ylabel;
		picture_ = new TimeSeriesDemo(chartName_, xlabel_, ylabel_);
	}
	
	public void outputProcess() throws InterruptedException
	{
		Object event = inValue_.getEvent();
		Date timeTag = inValue_.getTimeTag();
		events_[position_] = (Double)event;
		timeTags_[position_] = timeTag;
		position_++;
		if(position_ >= bufferSize_)
		{
			TimeSeries ts = new TimeSeries("Time Serie " + 1 , Day.class);
			for(int i=0; i<bufferSize_; i++)
			{
				ts.add(new Day(timeTags_[i]), (Double)events_[i]);
			}
			TimeSeriesCollection dataset = new TimeSeriesCollection();
			dataset.addSeries(ts);
			picture_.doPaint(dataset);
			position_ = 0;
		}
	}
}